package com.danco.training.zaneuskaya.faces;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.danco.training.zaneuskaya.domain.Message;
import com.danco.training.zaneuskaya.service.MessageService;
import com.danco.training.zaneuskaya.service.UserService;
import com.danco.training.zaneuskaya.service.exception.UserException;

/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Component
@Scope("request")
public class MessageBean implements Serializable {

	private static final String MESSAGES_MESSAGES = "/messages/messages";

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Logger LOGGER = Logger.getLogger(MessageBean.class);

	private int receiverId;

	@Autowired
	private UserService userService;

	@Autowired
	private MessageService messageService;

	@Autowired
	private UserBean userBean;

	private List<Message> messageList;

	private List<Message> sendedMessages;

	private List<Message> receivedMessages;

	private String currentText;

	private String subject;

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public String getCurrentText() {
		return currentText;
	}

	public void setCurrentText(String currentText) {
		this.currentText = currentText;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public List<Message> getSendedMessages() {
		sendedMessages = messageService.getUserSendedMessageList(userBean
				.getSelf());
		return sendedMessages;

	}

	public void setSendedMessages(List<Message> sendedMessages) {
		this.sendedMessages = sendedMessages;
	}

	public List<Message> getReceivedMessages() {
		receivedMessages = messageService.getUserReceivedMessageList(userBean
				.getSelf());
		return receivedMessages;
	}

	public void setReceivedMessages(List<Message> receivedMessages) {
		this.receivedMessages = receivedMessages;
	}

	public List<Message> getMessageList() {
		messageList = messageService.getUserMessageList(userBean.getSelf());
		return messageList;
	}

	public void setMessageList(List<Message> messageList) {
		this.messageList = messageList;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public MessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public String newMessage() {
		addMessage("New message");

		return "/messages/new";
	}

	public void addMessage(String summary) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				summary, null);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public String saveMessage() {
		Message m = new Message();
		m.setSender(userBean.getSelf());
		m.setReceiver(userService.getEntityById(receiverId));
		m.setMessageText(currentText);
		m.setMessageSubject(subject);
		m.setMessageTime(new Date());

		try {
			messageService.createEntity(m);
		} catch (UserException e) {
			LOGGER.error(e.getMessage(), e);
		}

		return MESSAGES_MESSAGES;
	}

	public int getReceiverId() {
		return receiverId;
	}

	public void setReceiverId(int receiverId) {
		this.receiverId = receiverId;
	}

}
